
<div id="main">
    <div id="contents">
        <form method="post" name="security_form">
            <fieldset>
                <legend id="ssidindex"><label sid="menu.security"></label></legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" width="100%" cellspacing="0" cellpadding="0" border="0">
                        <tbody>
                        <tr>
                            <td class="frame_tab_left">
                                <label for="id2201">
                                    <label sid="secure.security_mode"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <select id="security_mode" onchange="securityMode()" name="security_mode">
                                    <option sid="station.none" value="OPEN"></option>
                                    <option value="WEP">WEPAUTO</option>
                                    <option value="WPAPSK">WPA-PSK</option>
                                    <option value="WPA2PSK">WPA2-PSK</option>
                                    <option value="WPAPSKWPA2PSK">WPA/WPA2-PSK</option>
                                </select>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <fieldset id="div_wep" name="div_wep" style="display: none;">
                <legend id="secureWEP">
                    <label sid="secure.wep"></label>
                </legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" width="100%" cellspacing="0" cellpadding="0" border="0">
                        <tbody>
                        <tr>
                            <td>
                                <label for="id2232">
                                    <label sid="secure.wep_default_key"></label>
                                </label>
                                <span style="padding-left:10px;"></span>
                                <select id="id2232" name="id2232">
                                    <option id="secureWEPDefaultKey1" sid="secure.wep_default_key1" value="1"></option>
                                    <option id="secureWEPDefaultKey2" sid="secure.wep_default_key2" value="2"></option>
                                    <option id="secureWEPDefaultKey3" sid="secure.wep_default_key3" value="3"></option>
                                    <option id="secureWEPDefaultKey4" sid="secure.wep_default_key4" value="4"></option>
                                </select>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                    <table class="cmenutitle" width="100%" cellspacing="0" cellpadding="0" border="0">
                        <tbody>
                        <tr>
                            <th class="frame_tab_center">ID</th>
                            <th class="frame_tab_center">
                                <label sid="secure.wpa_pass_phrase"></label>
                            </th>
                            <th class="frame_tab_center">
                                <label sid="secure.lengths"></label>
                            </th>
                            <th class="frame_tab_center">
                                <label sid="secure.format"></label>
                            </th>
                        </tr>
                        <tr>
                            <td class="frame_tab_center">
                                <label for="id2234">1</label>
                            </td>
                            <td class="frame_tab_center">
                                <input id="id2234" value="" maxlength="26" name="id2234">
                            </td>
                            <td class="frame_tab_center">
                                <select id="length1" name="length1">
                                    <option value="64">64 bits</option>
                                    <option value="128">128 bits</option>
                                </select>
                            </td>
                            <td class="frame_tab_center">
                                <select id="id2233" name="id2233">
                                    <option value="1">ASCII</option>
                                    <option value="0">Hex</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_center">
                                <label for="id2236">2</label>
                            </td>
                            <td class="frame_tab_center">
                                <input id="id2236" value="" maxlength="26" name="id2236">
                            </td>
                            <td class="frame_tab_center">
                                <select id="length2" name="length2">
                                    <option value="64">64 bits</option>
                                    <option value="128">128 bits</option>
                                </select>
                            </td>
                            <td class="frame_tab_center">
                                <select id="id2235" name="id2235">
                                    <option value="1">ASCII</option>
                                    <option value="0">Hex</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_center">
                                <label for="id2238">3</label>
                            </td>
                            <td class="frame_tab_center">
                                <input id="id2238" value="" maxlength="26" name="id2238">
                            </td>
                            <td class="frame_tab_center">
                                <select id="length3" name="length3">
                                    <option value="64">64 bits</option>
                                    <option value="128">128 bits</option>
                                </select>
                            </td>
                            <td class="frame_tab_center">
                                <select id="id2237" name="id2237">
                                    <option value="1">ASCII</option>
                                    <option value="0">Hex</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="frame_tab_center">
                                <label for="id2240">4</label>
                            </td>
                            <td class="frame_tab_center">
                                <input id="id2240" value="" maxlength="26" name="id2240">
                            </td>
                            <td class="frame_tab_center">
                                <select id="length4" name="length4">
                                    <option value="64">64 bits</option>
                                    <option value="128">128 bits</option>
                                </select>
                            </td>
                            <td class="frame_tab_center">
                                <select id="id2239" name="id2239">
                                    <option value="1">ASCII</option>
                                    <option value="0">Hex</option>
                                </select>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <fieldset id="div_wpa" name="div_wpa" style="display: none;">
                <legend id="wpa">WPA-PSK</legend>
                <div class="frame_tab_layer">
                    <table class="cmenutitle" width="100%" cellspacing="0" cellpadding="0" border="0">
                        <tbody>
                        <tr id="div_wpa_algorithms" name="div_wpa_algorithms">
                            <td class="frame_tab_left">
                                <label for="id2227">
                                    <label sid="secure.wpa_algorithm"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id2227_1" type="radio" checked="" value="TKIP" name="id2227">
                                TKIP &nbsp;
                                <input id="id2227_2" type="radio" value="AES" name="id2227">
                                AES &nbsp;
<span id="id2227_tkip">
<input id="id2227_3" type="radio" value="TKIPAES" name="id2227">
TKIP&amp;AES &nbsp;
</span>
                            </td>
                        </tr>
                        <tr id="wpa_passphrase" name="wpa_passphrase">
                            <td class="frame_tab_left">
                                <label for="id2231">
                                    <label sid="secure.wpa_pass_phrase"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input id="id2231" value="" maxlength="64" size="28" name="id2231">
                            </td>
                        </tr>
                        <tr id="tr2228" name="tr2228">
                            <td class="frame_tab_left">
                                <label for="id2228">
                                    <label sid="secure.wpa_key_renew_interval"></label>
                                </label>
                            </td>
                            <td class="frame_tab_right">
                                <input name="id2228" id="id2228" size="6" maxlength="7" value="3600">
                                <label sid="ext_reg.table1_td2_note"></label>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </fieldset>
            <div class="submitFooter">
                <script type="text/javascript">

                    var buttonInfo = submitFooterButton(1,0);
                    $(".submitFooter").html(buttonInfo);

                </script>
            </div>
        </form>
    </div>
</div>
<br>

<div id="helpContainer">
    <div id="help">
        <div>
            <h2>
                <label sid="share.help"></label>
            </h2>
        </div>
        <div id="helpBody">
        <dl>
            <dt class="term">
                <label sid="secure.ssid_title"></label>
            </dt>
            <dd class="definition">
                <label sid="secure.ssid_introduction"></label>
            </dd>
            <dt class="term">
                <label sid="secure.wpa_pass_phrase"></label>
            </dt>
            <dd class="definition">
                <label sid="secure.web_key_note"></label>
            </dd>

        </dl>
        </div>
    </div>
</div>

<script type="text/javascript">


var nonames = new Array("security_mode", "submit", "length1", "length2", "length3", "length4");
var sendtext = getSendText(nonames);
sendtext += "&id=2164&id=2226&id=2227";
DataRequest("get_json", sendtext, function (data) {
    getResult(nonames, data);
    var value2226 = data["id2226"];
    var value2227 = data["id2227"];
    var s_mode = "";
    if (value2226 == "OPEN" && value2227 == "NONE") {
        s_mode = "OPEN";
    } else if (value2226 == "OPEN" && value2227 == "WEP") {
        s_mode = "WEP";
    } else {
        s_mode = value2226;
    }
    listselect("security_mode", s_mode);
    var ssidindex = data["id2164"];
    //$("#ssidindex").html(ssidindex);
    securityMode();
    if ($("#id2234").val().length == 5 || $("#id2234").val().length == 10) {
        listselect("length1", "64");
    } else if ($("#id2234").val().length == 13 || $("#id2234").val().length == 26) {
        listselect("length1", "128");
    }

    if ($("#id2236").val().length == 5 || $("#id2236").val().length == 10) {
        listselect("length2", "64");
    } else if ($("#id2236").val().length == 13 || $("#id2236").val().length == 26) {
        listselect("length2", "128");
    }

    if ($("#id2238").val().length == 5 || $("#id2238").val().length == 10) {
        listselect("length3", "64");
    } else if ($("#id2238").val().length == 13 || $("#id2238").val().length == 26) {
        listselect("length3", "128");
    }

    if ($("#id2240").val().length == 5 || $("#id2240").val().length == 10) {
        listselect("length4", "64");
    } else if ($("#id2240").val().length == 13 || $("#id2240").val().length == 26) {
        listselect("length4", "128");
    }
    if (value2227 == "NONE") {
        $("#id2227_1").attr("checked", true);
    }
});

$("[name=submit]").click(function () {
    $(".errormsg").html('');
    if (checkData()) {
        var settext = '';
        var EncrypType = "";
        var securitymode = $("#security_mode").val();
        if (securitymode == "OPEN") {
            settext += "&id2226=OPEN";
            settext += "&id2227=NONE";
            EncrypType = "NONE";
        } else if (securitymode == "WEP") {
            settext += "&id2226=OPEN";
            settext += "&id2227=WEP";
            EncrypType = "WEP";
            settext += "&id2232=" + $("#id2232").val();
            settext += "&id2233=" + $("#id2233").val();
            settext += "&id2234=" + $("#id2234").val();
            settext += "&id2235=" + $("#id2235").val();
            settext += "&id2236=" + $("#id2236").val();
            settext += "&id2237=" + $("#id2237").val();
            settext += "&id2238=" + $("#id2238").val();
            settext += "&id2239=" + $("#id2239").val();
            settext += "&id2240=" + $("#id2240").val();
        } else if (securitymode == "WPAPSK" || securitymode == "WPA2PSK" || securitymode == "WPAPSKWPA2PSK") {
            settext += "&id2226=" + securitymode;
            if ($("#id2227_1").attr("checked")) {
                EncrypType = $("#id2227_1").val();
                settext += "&id2227=" + $("#id2227_1").val();
            } else if ($("#id2227_2").attr("checked")) {
                EncrypType = $("#id2227_2").val();
                settext += "&id2227=" + $("#id2227_2").val();
            }
            if ($("#id2227_3").attr("checked")) {
                EncrypType = $("#id2227_3").val();
                settext += "&id2227=" + $("#id2227_3").val();
            }
            settext += "&id2233=" + $("#id2233").val();
            settext += "&id2234=" + $("#id2234").val();
            settext += "&id2235=" + $("#id2235").val();
            settext += "&id2236=" + $("#id2236").val();
            settext += "&id2237=" + $("#id2237").val();
            settext += "&id2238=" + $("#id2238").val();
            settext += "&id2239=" + $("#id2239").val();
            settext += "&id2240=" + $("#id2240").val();
            settext += "&id2228=" + $("#id2228").val();
            settext += "&id2231=" + $("#id2231").val();
            if (EncrypType !== "NONE") {
                settext += "&id2229=TIME";
            }
        }
        //alert(settext);
        submitform(modules["set_data"], settext);
    }
});

function securityMode() {
    var security_mode = $("#security_mode").val();
    if (security_mode == "WEP") {
        $("#div_wep").show();
        $("#div_wpa").hide();
        $("#id2227_tkip").hide();
    } else if (security_mode == "WPAPSK") {
        $("#wpa").html("WPA-PSK");
        $("#div_wep").hide();
        $("#div_wpa").show();
        $("#id2227_tkip").hide();
       /* $("#id2227_1").attr("checked", true);*/
    } else if (security_mode == "WPAPSKWPA2PSK" || security_mode == "WPA2PSK") {
        if (security_mode == "WPAPSKWPA2PSK") {
            $("#wpa").html("WPA/WPA2-PSK");
        } else {
            $("#wpa").html("WPA2-PSK");
        }
        $("#div_wep").hide();
        $("#div_wpa").show();
        $("#id2227_tkip").show();
        $("#id2227_tkip").attr("checked", true);
    } else {
        $("#div_wep").hide();
        $("#div_wpa").hide();
        $("#id2227_tkip").hide();
    }
}

function atoi(str, num) {
    i = 1;
    if (num != 1) {
        while (i != num && str.length != 0) {
            if (str.charAt(0) == '.') {
                i++;
            }
            str = str.substring(1);
        }
        if (i != num)
            return -1;
    }

    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) == '.') {
            str = str.substring(0, i);
            break;
        }
    }
    if (str.length == 0)
        return -1;
    return parseInt(str, 10);
}

function checkHex(str) {
    var len = str.length;

    for (var i = 0; i < str.length; i++) {
        if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') ||
                (str.charAt(i) >= 'a' && str.charAt(i) <= 'f') ||
                (str.charAt(i) >= 'A' && str.charAt(i) <= 'F')) {
            continue;
        } else
            return false;
    }
    return true;
}

function checkInjection(str) {
    var len = str.length;
    for (var i = 0; i < str.length; i++) {
        if (str.charAt(i) == '\r' || str.charAt(i) == '\n') {
            return false;
        } else
            continue;
    }
    return true;
}

function checkAllNum(str) {
    for (var i = 0; i < str.length; i++) {
        if ((str.charAt(i) >= '0' && str.charAt(i) <= '9') || (str.charAt(i) == '.' ))
            continue;
        return false;
    }
    return true;
}


function checkData() {
    var securitymode = $("#security_mode").val();
    if (securitymode == "WEP") {
        if (!check_Wep(securitymode))
            return false;
    } else if (securitymode == "WPAPSK" || securitymode == "WPA2PSK" || securitymode == "WPAPSKWPA2PSK") {
        var keyvalue = $("#id2231").val();
        if (keyvalue.length == 0) {
            alert(secure.msg1);
            return false;
        }

        if (keyvalue.length < 8) {
            alert(secure.msg2);
            return false;
        }

        if (keyvalue.length == 64 && checkHex(keyvalue) == false) {
            alert(secure.msg3);
            return false;
        }

        if (checkInjection($("#id2231").val()) == false) {
            alert(secure.msg4);
            return false;
        }

        if (document.security_form.id2227[0].checked != true &&
                document.security_form.id2227[1].checked != true &&
                document.security_form.id2227[2].checked != true) {
            alert(secure.msg5);
            return false;
        }
        if (check_wpa() == false)
            return false;
    }
    return true;
}

function check_wpa() {
    if (document.security_form.id2227[0].checked != true &&
            document.security_form.id2227[1].checked != true &&
            document.security_form.id2227[2].checked != true) {
        alert(secure.msg5);
        return false;
    }

    if ((document.security_form.security_mode.value == "WPA" ||
            document.security_form.security_mode.value == "WPAPSK") &&
            document.security_form.id2227[2].checked == true) {
        document.security_form.id2227[0].checked = true;
        document.security_form.id2227[1].checked = false;
        document.security_form.id2227[2].checked = false;
    }

    if (checkAllNum(document.security_form.id2228.value) == false) {
        alert(secure.msg7);
        return false;
    }
    if (document.security_form.id2228.value == 0) {
        alert(secure.msg8);
    }
    return true;
}


function check_Wep(securitymode) {
    var defaultid = $("#id2232").val();
    var key_input;

    if (defaultid == 1)
        var keyvalue = $("#id2234").val();
    else if (defaultid == 2)
        var keyvalue = $("#id2236").val();
    else if (defaultid == 3)
        var keyvalue = $("#id2238").val();
    else if (defaultid == 4)
        var keyvalue = $("#id2240").val();

    if (keyvalue.length == 0 && securitymode == "WEP") {
        alert(secure.msg9 + defaultid + ' !');
        return false;
    }

    var keylength = $("#id2234").val().length;
    var lengths = $("#length1").val();
    if (keylength != 0) {
        if (document.security_form.id2233.options.selectedIndex == 0 && lengths == "64") {
            if (keylength != 5) {
                alert(secure.msg10);
                return false;
            }
            if (checkInjection($("#id2234").val()) == false) {
                alert(secure.msg11);
                return false;
            }
        } else if (document.security_form.id2233.options.selectedIndex == 0 && lengths == "128") {
            if (keylength != 13) {
                alert(secure.msg12);
                return false;
            }
            if (checkInjection($("#id2234").val()) == false) {
                alert(secure.msg11);
                return false;
            }
        } else if (document.security_form.id2233.options.selectedIndex == 1 && lengths == "64") {
            if (keylength != 10) {
                alert(secure.msg14);
                return false;
            }
            if (checkHex($("#id2234").val()) == false) {
                alert(secure.msg15);
                return false;
            }
        } else if (document.security_form.id2233.options.selectedIndex == 1 && lengths == "128") {
            if (keylength != 26) {
                alert(secure.msg16);
                return false;
            }
            if (checkHex($("#id2234").val()) == false) {
                alert(secure.msg15);
                return false;
            }
        }
    }

    keylength = $("#id2236").val().length;
    lengths = $("#length2").val();
    if (keylength != 0) {
        if (document.security_form.id2235.options.selectedIndex == 0 && lengths == "64") {
            if (keylength != 5) {
                alert(secure.msg18);
                return false;
            }
            if (checkInjection($("#id2236").val()) == false) {
                alert(secure.msg19);
                return false;
            }
        } else if (document.security_form.id2235.options.selectedIndex == 0 && lengths == "128") {
            if (keylength != 13) {
                alert(secure.msg20);
                return false;
            }
            if (checkInjection($("#id2236").val()) == false) {
                alert(secure.msg19);
                return false;
            }
        } else if (document.security_form.id2235.options.selectedIndex == 1 && lengths == "64") {
            if (keylength != 10) {
                alert(secure.msg21);
                return false;
            }
            if (checkHex($("#id2236").val()) == false) {
                alert(secure.msg22);
                return false;
            }
        } else if (document.security_form.id2235.options.selectedIndex == 1 && lengths == "128") {
            if (keylength != 26) {
                alert(secure.msg23);
                return false;
            }
            if (checkHex($("#id2236").val()) == false) {
                alert(secure.msg22);
                return false;
            }
        }
    }

    keylength = $("#id2238").val().length;
    lengths = $("#length3").val();
    if (keylength != 0) {
        if (document.security_form.id2237.options.selectedIndex == 0 && lengths == "64") {
            if (keylength != 5) {
                alert(secure.msg24);
                return false;
            }
            if (checkInjection($("#id2238").val()) == false) {
                alert(secure.msg25);
                return false;
            }
        } else if (document.security_form.id2237.options.selectedIndex == 0 && lengths == "128") {
            if (keylength != 13) {
                alert(secure.msg26);
                return false;
            }
            if (checkInjection($("#id2238").val()) == false) {
                alert(secure.msg25);
                return false;
            }
        } else if (document.security_form.id2237.options.selectedIndex == 1 && lengths == "64") {
            if (keylength != 10) {
                alert(secure.msg27);
                return false;
            }
            if (checkHex($("#id2238").val()) == false) {
                alert(secure.msg28);
                return false;
            }
        } else if (document.security_form.id2237.options.selectedIndex == 1 && lengths == "128") {
            if (keylength != 26) {
                alert(secure.msg29);
                return false;
            }
            if (checkHex($("#id2238").val()) == false) {
                alert(secure.msg28);
                return false;
            }
        }
    }

    keylength = $("#id2240").val().length;
    lengths = $("#length4").val();
    if (keylength != 0) {
        if (document.security_form.id2239.options.selectedIndex == 0 && lengths == "64") {
            if (keylength != 5) {
                alert(secure.msg30);
                return false;
            }
            if (checkInjection($("#id2240").val()) == false) {
                alert(secure.msg31);
                return false;
            }
        } else if (document.security_form.id2239.options.selectedIndex == 0 && lengths == "128") {
            if (keylength != 13) {
                alert(secure.msg32);
                return false;
            }
            if (checkInjection($("#id2240").val()) == false) {
                alert(secure.msg31);
                return false;
            }
        } else if (document.security_form.id2239.options.selectedIndex == 1 && lengths == "64") {
            if (keylength != 10) {
                alert(secure.msg33);
                return false;
            }
            if (checkHex($("#id2240").val()) == false) {
                alert(secure.msg34);
                return false;
            }
        } else if (document.security_form.id2239.options.selectedIndex == 1 && lengths == "128") {
            if (keylength != 26) {
                alert(secure.msg35);
                return false;
            }
            if (checkHex($("#id2240").val()) == false) {
                alert(secure.msg34);
                return false;
            }
        }
    }
    return true;
}

</script>
